package cn.net.sdjx.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

public class ValidateCode {
	/*// 图片的宽度。  
    private int width = 160;  
    // 图片的高度。  
    private int height = 40;  
    // 验证码字符个数  
    private int codeCount = 5;  
    // 验证码干扰线数  
    private int lineCount = 150;  
    // 验证码  
    private String code = null;  
    // 验证码图片Buffer  
    private BufferedImage buffImg = null;  

    // 验证码范围,去掉0(数字)和O(拼音)容易混淆的(小写的1和L也可以去掉,大写不用了)  
    private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',  
            'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',  
            'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};  

    *//** 
     * 默认构造函数,设置默认参数 
     *//*  
    public ValidateCode() {  
        this.createCode();  
    }  

    *//** 
     * @param width  图片宽 
     * @param height 图片高 
     *//*  
    public ValidateCode(int width, int height) {  
        this.width = width;  
        this.height = height;  
        this.createCode();  
    }  

    *//** 
     * @param width     图片宽 
     * @param height    图片高 
     * @param codeCount 字符个数 
     * @param lineCount 干扰线条数 
     *//*  
    public ValidateCode(int width, int height, int codeCount, int lineCount) {  
        this.width = width;  
        this.height = height;  
        this.codeCount = codeCount;  
        this.lineCount = lineCount;  
        this.createCode();  
    }  

    public void createCode() {  
        int x = 0, fontHeight = 0, codeY = 0;  
        int red = 0, green = 0, blue = 0;  

        x = width / (codeCount + 2);//每个字符的宽度(左右各空出一个字符)  
        fontHeight = height - 2;//字体的高度  
        codeY = height - 4;  

        // 图像buffer  
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics2D g = buffImg.createGraphics();  
        // 生成随机数  
        Random random = new Random();  
        // 将图像填充为白色  
        g.setColor(Color.WHITE);  
        g.fillRect(0, 0, width, height);  
        // 创建字体,可以修改为其它的  
        Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);  
//        Font font = new Font("Times New Roman", Font.ROMAN_BASELINE, fontHeight);  
        g.setFont(font);  

        for (int i = 0; i < lineCount; i++) {  
            // 设置随机开始和结束坐标  
            int xs = random.nextInt(width);//x坐标开始  
            int ys = random.nextInt(height);//y坐标开始  
            int xe = xs + random.nextInt(width / 8);//x坐标结束  
            int ye = ys + random.nextInt(height / 8);//y坐标结束  

            // 产生随机的颜色值，让输出的每个干扰线的颜色值都将不同。  
            red = random.nextInt(255);  
            green = random.nextInt(255);  
            blue = random.nextInt(255);  
            g.setColor(new Color(red, green, blue));  
            g.drawLine(xs, ys, xe, ye);  
        }  

        // randomCode记录随机产生的验证码  
        StringBuffer randomCode = new StringBuffer();  
        // 随机产生codeCount个字符的验证码。  
        for (int i = 0; i < codeCount; i++) {  
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);  
            // 产生随机的颜色值，让输出的每个字符的颜色值都将不同。  
            red = random.nextInt(255);  
            green = random.nextInt(255);  
            blue = random.nextInt(255);  
            g.setColor(new Color(red, green, blue));  
            g.drawString(strRand, (i + 1) * x, codeY);  
            // 将产生的四个随机数组合在一起。  
            randomCode.append(strRand);  
        }  
        // 将四位数字的验证码保存到Session中。  
        code = randomCode.toString();  
    }  

    public void write(String path) throws IOException {  
        OutputStream sos = new FileOutputStream(path);  
        this.write(sos);  
    }  

    public void write(OutputStream sos) throws IOException {  
        ImageIO.write(buffImg, "png", sos);  
        sos.close();  
    }  

    public BufferedImage getBuffImg() {  
        return buffImg;  
    }  

    public String getCode() {  
        return code;  
    }  

    *//** 
     * 测试函数,默认生成到d盘 
     * @param args 
     *//*  
    public static void main(String[] args) {  
        ValidateCode vCode = new ValidateCode(160,40,5,150);  
        try {  
            String path="F:/"+"code"+".png";  
            System.out.println(vCode.getCode()+" >"+path);  
            vCode.write(path);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  */
	
	
	private BufferedImage image;// 图像 
	  private String str;// 验证码 
	  private static char code[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray(); 
	  
	  public static final String SESSION_CODE_NAME="code"; 
	    
	  private ValidateCode() { 
	    init();// 初始化属性 
	  } 
	  
	  /* 
	   * 取得RandomNumUtil实例 
	   */ 
	  public static ValidateCode Instance() { 
	    return new ValidateCode(); 
	  } 
	  
	  /* 
	   * 取得验证码图片 
	   */ 
	  public BufferedImage getImage() { 
	    return this.image; 
	  } 
	  
	  /* 
	   * 取得图片的验证码 
	   */ 
	  public String getString() { 
	    return this.str; 
	  } 
	  
	  private void init() { 
	    // 在内存中创建图象 
	    int width = 85, height = 20; 
	    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
	    // 获取图形上下文 
	    Graphics g = image.getGraphics(); 
	    // 生成随机类 
	    Random random = new Random(); 
	    // 设定背景色 
	    g.setColor(getRandColor(200, 250)); 
	    g.fillRect(0, 0, width, height); 
	    // 设定字体 
	    g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); 
	    // 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到 
	    g.setColor(getRandColor(160, 200)); 
	    for (int i = 0; i < 155; i++) { 
	      int x = random.nextInt(width); 
	      int y = random.nextInt(height); 
	      int xl = random.nextInt(12); 
	      int yl = random.nextInt(12); 
	      g.drawLine(x, y, x + xl, y + yl); 
	    } 
	    // 取随机产生的认证码(4位数字) 
	    String sRand = ""; 
	    for (int i = 0; i < 4; i++) { 
	      String rand = String.valueOf(code[random.nextInt(code.length)]); 
	      sRand += rand; 
	      // 将认证码显示到图象中 
	      g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); 
	      // 调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生成 
	      g.drawString(rand, 13 * i + 6, 16); 
	    } 
	    // 赋值验证码 
	    this.str = sRand; 
	  
	    // 图象生效 
	    g.dispose(); 
	    // ByteArrayInputStream input = null; 
	    // ByteArrayOutputStream output = new ByteArrayOutputStream(); 
	    // try { 
	    // ImageOutputStream imageOut = ImageIO.createImageOutputStream(output); 
	    // ImageIO.write(image, "JPEG", imageOut); 
	    // imageOut.close(); 
	    // input = new ByteArrayInputStream(output.toByteArray()); 
	    // } catch (Exception e) { 
	    // System.out.println("验证码图片产生出现错误：" + e.toString()); 
	    // } 
	    // this.image = input 
	    this.image = image;/* 赋值图像 */ 
	  } 
	  
	  /* 
	   * 给定范围获得随机颜色 
	   */
	  private Color getRandColor(int fc, int bc) { 
	    Random random = new Random(); 
	    if (fc > 255) 
	      fc = 255; 
	    if (bc > 255) 
	      bc = 255; 
	    int r = fc + random.nextInt(bc - fc); 
	    int g = fc + random.nextInt(bc - fc); 
	    int b = fc + random.nextInt(bc - fc); 
	    return new Color(r, g, b); 
	  } 
	}

